package com.rongc.client.freight.business.waybill.view.mvp;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.pay.DoAlipay;
import com.rongc.client.core.pay.alipay.util.OrderInfoUtil2_0;
import com.rongc.client.core.pay.wxpay.WxpayHelper;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.business.waybill.model.WayBillDetailBean;
import com.rongc.client.freight.business.waybill.request.api.WaybillAddpriceApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillCancelApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillDetailApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillPayAliApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillPayQianbaoApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillPaywxApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillStatusApi;

import org.json.JSONObject;

import java.util.Map;

import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.DTH_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.ROAD_STATUS;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.YAJIN_STATUS;

/**
 * Created by Bruce on 2017/7/13.
 */
public class QuestionPresenter implements QuestionContract.Presenter<WayBillDetailBean>{
    @NonNull
    private final QuestionContract.View mView;

    public QuestionPresenter(@NonNull QuestionContract.View mView) {
        this.mView = mView;
        this.mView.setPresenter(this);
    }
    @Override
    public void start() {}


    @Override
    public void QianbaoPay(String type, String wid, String zhifu_pwd) {
        WaybillPayQianbaoApi qianbaopay = new WaybillPayQianbaoApi(new WaybillPayQianbaoApi.WaybillPayQianbaoParams(
                type,
                wid,zhifu_pwd),
                respQianbaoPayListener, errorListener);
        RequestManager.getInstance().call(qianbaopay);
    }

    @Override
    public void Alipay(String type, String wid) {
        WaybillPayAliApi pay = new WaybillPayAliApi(new WaybillPayAliApi.WaybillPayParams(
                type,
                wid),
                respAliPayListener, errorListener);
        RequestManager.getInstance().call(pay);
    }

    @Override
    public void WxPay(String type, String wid) {
                        WaybillPaywxApi wxpay = new WaybillPaywxApi(new WaybillPaywxApi.WaybillPaywxParams(
                                type,
                                wid),
                                respwxPayListener, errorListener);
                        RequestManager.getInstance().call(wxpay);
    }

    @Override
    public void changeStatus(String status, String wid) {
        WaybillStatusApi p = new WaybillStatusApi(new WaybillStatusApi.WaybillStatusParams(
                UniApplication.getInstance().getUserInfo().getUserId(),
                wid, status),
                respWaybillStatusListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    public void changeStatusByPay(String status){
        switch (StringUtils.parseInt(mView.getStatus())){
            case DTH_STATUS:
                mView.showDialog("showTH");
                break;
            case ROAD_STATUS:
                mView.showDialog("showHD");
                break;
        }
        mView.setStatus(status);
        mView.refreshView();
    }

    @Override
    public void refreshWaybill(String wid) {
        WaybillDetailApi p = new WaybillDetailApi(new WaybillDetailApi.WaybillDetailParams(
                wid),
                respWaybillListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void jiajia(String price, String wid) {
        WaybillAddpriceApi p = new WaybillAddpriceApi(new WaybillAddpriceApi.WaybillAddpriceParams(
                wid, price),
                respPriceListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void cancel(String wid) {
        //  取消绑定
        WaybillCancelApi p = new WaybillCancelApi(new WaybillCancelApi.WaybillCancelParams(
                wid),
                respCancelListener, errorListener);

        RequestManager.getInstance().call(p);
    }


    Response.Listener<JSONObject> respPriceListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
//                mView.refreshView();
                refreshWaybill(mView.getWayBillid());

                mView.hideDialog("inputDialog");
            } else {
                mView.complateLoaded();
            }
        }
    };

    Response.Listener<NullResult> respCancelListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                mView.hideDialog("caneldialog");
            } else {
                mView.complateLoaded();
            }
        }
    };

    Response.Listener<NullResult> respWaybillStatusListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            mView.complateLoaded();
            if (HandleCode.requestSuccess()) {
                switch (StringUtils.parseInt(mView.getStatus())){
                    case DTH_STATUS:
                        mView.showDialog("showTH");
                        break;
                    case ROAD_STATUS:
                        mView.showDialog("showHD");
                        break;
                }

                refreshWaybill(mView.getWayBillid());
            } else {
            }
        }
    };

    Response.Listener<JSONObject> respQianbaoPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {

                if("1".equals(response.optString("status"))){
                    mView.hideDialog("popupWindowPay");
                    changeStatusByPay(getStatus()+"");
                }else if("2".equals(response.optString("status"))){
                    mView.showDialog("popupWindowPayWay");
                    ActivityUtils.toast(response.optString("code"));
                }else{
                    mView.hideDialog("popupWindowPay");
                    ActivityUtils.toast(response.optString("code"));
                }

            } else {
                mView.complateLoaded();
            }
        }
    };

    Response.Listener<JSONObject> respAliPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                mView.hideDialog("popupWindowPay");
                Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(response);
                String orderParam = OrderInfoUtil2_0.buildOrderParam(params);

                String sign = OrderInfoUtil2_0.getSign(params);
                final String orderInfo = orderParam + "&" + sign;
                new DoAlipay().payAlipay(orderInfo, (Activity) mView, new DoAlipay.OnPayCompleteResyltListener() {
                    @Override
                    public void OnResult(String resultStr, String txt, boolean result) {
                        if(result){
                            //  支付成功
                            changeStatusByPay(getStatus()+"");
                            ActivityUtils.toast("支付成功");
                        }else{
                            //  支付失败
                            ActivityUtils.toast("支付失败");
                        }
                    }
                });
            } else {
                mView.complateLoaded();
            }
        }
    };

    Response.Listener<JSONObject> respwxPayListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                mView.hideDialog("popupWindowPay");
                Map<String,String> wxstr = null;
                wxstr = GsonHelper.jsonToMap(response.toString(),new TypeToken<Map<String, String>>(){});

                WxpayHelper.getInstent((Activity) mView);
                WxpayHelper.pay(wxstr, (Activity) mView, new WxpayHelper.onWxpayPaylistener() {
                    @Override
                    public void payResult(String result) {
                        /*if(result){
                            //  支付成功
                            changeStatusByPay(getStatus()+"");
                            ActivityUtils.toast("支付成功");
                        }else{
                            //  支付失败
                            ActivityUtils.toast("支付失败");
                        }*/
                    }
                });

            } else {
                mView.complateLoaded();
            }
        }
    };

    int getStatus(){
        int status = DTH_STATUS;
        if("1".equals(mView.getPayType())){
            status = YAJIN_STATUS;
        }else if("3".equals(mView.getPayType())) {
            status = DTH_STATUS;
            mView.showDialog("showPayResultYJ");
        }
        return status;
    }

    Response.Listener<WayBillDetailBean> respWaybillListener = new Response.Listener<WayBillDetailBean>() {

        @Override
        public void onResponse(WayBillDetailBean response) {
            if (HandleCode.requestSuccess()) {
                mView.doShow(response);
            } else {
                mView.complateLoaded();
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            mView.complateLoaded();
        }
    };
}
